//
//  CellOfOtherDestination.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/7.
//  Copyright © 2016年 KX. All rights reserved.
#import "Masonry.h"
#import "FreeTravelMacro.h"
#import "CellOfOtherDestination.h"
#import "CellOfCellOfOtherDestination.h"
#import "CellOfOtherDestinationHead.h"
#import "CountryViewController.h"
#import "DestinationViewController.h"
@interface CellOfOtherDestination ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation CellOfOtherDestination
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCollectionView];
    }
    return self;
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.contentView.bounds collectionViewLayout:layout];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH-20, 44);
    self.collectionView.scrollEnabled = NO;
    self.collectionView.backgroundColor  = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[CellOfCellOfOtherDestination class] forCellWithReuseIdentifier:@"CellOfCellOfOtherDestination"];
    [self.collectionView registerClass:[CellOfOtherDestinationHead class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CellOfOtherDestinationHead"];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    CellOfOtherDestinationHead *head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CellOfOtherDestinationHead" forIndexPath:indexPath];
    head.model = self.model;
    
    return head;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, 50);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.model.country.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfCellOfOtherDestination *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfCellOfOtherDestination" forIndexPath:indexPath];
    cell.model = self.model.country[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CountryViewController *vc = [[CountryViewController alloc]init];
    
    ModelOfContinentsCountry *model = self.model.country[indexPath.row];
    vc.ID = model.ID;
    DestinationViewController *desVC = (DestinationViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
    [desVC.navigationController pushViewController:vc animated:YES];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.contentView.bounds;
}
- (void)setModel:(ModelOfContinents *)model{
    _model = model;
    [self.collectionView reloadData];
}

@end
